myApp.controller('LoginCtrl', ['$scope','tools','$cookies','$timeout','Session','$location','SweetAlert','NoticeServ','$http','$rootScope', function ($scope, tools,$cookies,$timeout,Session,$location,SweetAlert,NoticeServ,$http,$rootScope) {

	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		remember:false,
		rememberStuff:false,
		nameError:'',
		passwordError:'',
		init:function(){
			var data = $cookies.get('user');
			console.log(data);
			if(data){
				data=JSON.parse(data);
				this.name = data.name;
				this.password = data.password;
				this.remember = data.remember; 
				this.rememberStuff = true;
			}
		},
		isRemember:function(){			
			
			console.log(this.remember);
			if(this.remember){
				var data = {
					name:this.name,
					password:this.password,
					remember:true
				};

				$cookies.put('user',JSON.stringify(data));
				console.log($cookies.get('user'));
			}else{
				console.log('remove cookie');
				$cookies.remove('user');
			}
			
		},
		checkName:function(){
		
			console.log(this.name);
			console.log(this.oldName);
			if(this.name == '' || this.name == this.oldName){
				return;
			}
			
			var $this = this;
			tools.$post({
				url:'/users/queryByName',
				data:{
					name:this.name
				}
			}).then(function(resp){
				$this.oldName = $this.name;
				console.log(resp);
				if(resp.value.length == 0){
					$this.nameError = "该用户名不存在";
				}else{
					$this.nameError = "";
				}
				
			},function(errResp){

				console.log('error')
			});
		},
		login:function(){

			if(this.nameError !='' || this.name == '' || this.password == ''){
				return;
			}
			var $this = this;
			console.log(this.remember);
			tools.$post({
				url:'/users/login',
				// url:'proxy/marketconditions/user/userLogin.htm',
				data:{
					name:this.name,
					password:this.password
				}
			}).then(function(resp){
				console.log('++++')
				console.log(resp);
				$this.passwordError = "";
				$this.isRemember();
				Session.create(resp.value);
				$location.path('/share');
				NoticeServ.$broadcast('LOGIN_EV');
				// SweetAlert.swal({
				// 	title: "登录成功",
				// 	type: "success",
				// 	showCancelButton: false,
				// 	confirmButtonColor: "#DD6B55",
				// 	confirmButtonText: "去首页",
				// 	closeOnConfirm: true
				// },function(){ 
				// 	$timeout(function(){
				// 		$location.path('/share');
				// 		$rootScope.$broadcast('LOGIN_SUCCESS_EV');
				// 	},100);
				// });
			
			},function(errResp){
				console.log(errResp);


				if(errResp.message == "alreadyEnter"){
					SweetAlert.swal('','该用户已登录','error');
				}else{
					$this.passwordError = "密码错误";
					SweetAlert.swal('','账号或密码错误','error');
				}

				
				// console.log(errResp)
				console.log('error');
			});
		}
	};

	$scope.User.init();
}]);
directives.directive('usernameReg', function($q, $timeout,tools) {

    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
			// $(elm).bind('blur',function(ctrl){
				console.log('blur');

	            ctrl.$asyncValidators.username = function(modelValue, viewValue) {
	                console.log('modelValue:')
	                console.log(modelValue);
	                if (ctrl.$isEmpty(modelValue)) {
	                    // consider empty model valid
	                    return $q.resolve();
	                }
	                var def = $q.defer();
	                var value = modelValue;
					tools.$get({
						url:'/users/queryByName',
						data: {
							name:value
						}
					}).then(function(resp){
	                	console.log(resp);
	                	if(resp.value.length==0){
	                		console.log('0');
	                		def.reject();
	                	}else{
	                		console.log('1');
	                		def.resolve();
	                	}
	                	
	                },function(err){
	                	def.reject();
	                });
	                    
	                return def.promise;
	            };
	        // });  
        }  

    };
});